package com.zbcz.erp.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.zbcz.erp.ErpApplication
import com.zbcz.erp.bean.DoneJobBean
import com.zbcz.erp.network.ErpApi
import com.zbcz.erp.network.RetrofitManager
import com.zbcz.erp.sp.SharedPreferencesManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MainActivityViewModel : ViewModel() {


    private val mApi = RetrofitManager.getRetrofit()?.create(ErpApi::class.java)

    var mToDoListJobListLiveData: MutableLiveData<List<DoneJobBean?>?> = MutableLiveData()

    var mDoneJobListLiveData: MutableLiveData<List<DoneJobBean?>?> = MutableLiveData()

    var mDoingJobListLiveData: MutableLiveData<List<DoneJobBean?>?> = MutableLiveData()

    var mActPassLiveData: MutableLiveData<Pair<Boolean, String?>> = MutableLiveData()

    var mCancelLiveData: MutableLiveData<Boolean> = MutableLiveData()

    fun getToDoList() {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                val result = getToDoListSuspend()
                mToDoListJobListLiveData.postValue(result)
            }
        }
    }

    fun getDoneList() {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                val result = getDoneListSuspend()
                mDoneJobListLiveData.postValue(result)
            }
        }
    }

    fun getMyApplyList() {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                val result = getDoingProcessListSuspend()
                mDoingJobListLiveData.postValue(result)
            }
        }
    }


    private suspend fun getToDoListSuspend(): List<DoneJobBean>? {
        try {
            val result = mApi?.getToDoList(
                SharedPreferencesManager.getUserInfo(ErpApplication.mApplication)?.username ?: "",
                SharedPreferencesManager.getToken(ErpApplication.mApplication)
            )
            if (result?.code == 200) {
                return result.result
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }


    private suspend fun getDoneListSuspend(): List<DoneJobBean>? {
        try {
            val result =
                mApi?.getDoneList(token = SharedPreferencesManager.getToken(ErpApplication.mApplication))
            if (result?.code == 200) {
                return result.result
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    private suspend fun getDoingProcessListSuspend(): List<DoneJobBean>? {
        try {
            val result =
                mApi?.getDoingBusinessList(token = SharedPreferencesManager.getToken(ErpApplication.mApplication))
            if (result?.code == 200) {
                return result.result
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }


    fun cancelApply(
        id: String,
        procInstId: String
    ) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                mCancelLiveData.postValue(cancelApplySuspend(id, procInstId) != null)
            }
        }
    }

    private suspend fun cancelApplySuspend(
        id: String,
        procInstId: String,
    ): String? {
        try {
            val result = mApi?.cancel(id, procInstId)
            if (result?.code == 200) {
                return result.result
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    fun actPass(
        id: String,
        procInstId: String
    ) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                mActPassLiveData.postValue(actPassSuspend(id, procInstId))
            }
        }
    }


    private suspend fun actPassSuspend(
        id: String,
        procInstId: String,
    ): Pair<Boolean, String?> {
        try {
            val result = mApi?.actTaskPass(id, procInstId)
            return if (result?.code == 200) {
                Pair(true, result.message)
            } else {
                Pair(true, result?.message)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return return Pair(false, "")
    }

}